package com.yf.dxapp.module.app.controller.app;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.yf.dxapp.module.app.component.app.AppCustomerServiceComponent;
import com.yf.dxapp.module.app.controller.app.vo.AppAboutUsVo;
import com.yf.dxapp.module.app.dal.dataobject.appconfig.AppConfigDO;
import com.yf.dxapp.module.app.dal.dataobject.appuser.AppUserDO;
import com.yf.dxapp.module.app.service.appconfig.AppConfigService;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author yxmds
 */
@RestController
public class ApiAppMyController extends ApiAppBaseController {


    @Resource
    private AppConfigService appConfigService;

    @Resource
    private AppCustomerServiceComponent appCustomerServiceComponent;

    /**
     * POST /api/app/mini/my/aboutus : 关于我们
     */
    @RequestMapping(value = "/my/aboutus",
            method = RequestMethod.POST)
    @PermitAll
    public CommonResult aboutUs() {


        List<AppConfigDO> configList = appConfigService.findConfigByKey(Arrays.asList("投诉电话", "联系电话", "版权归属", "隐私协议地址", "用户协议地址", "工作时间"));
        if (configList.isEmpty()) {
            return error(-1, "获取不到app信息，请联系系统管理员");
        }
        Map<String, String> configMap = configList.stream().collect(Collectors.toMap(AppConfigDO::getName, AppConfigDO::getValue));
        AppAboutUsVo appAboutUsVo = new AppAboutUsVo();
        appAboutUsVo.setComplaintMobile(configMap.get("投诉电话"));
        appAboutUsVo.setConnectMobile(configMap.get("联系电话"));
        appAboutUsVo.setCopyright(configMap.get("版权归属"));
        appAboutUsVo.setPrivacyPolicyUrl(configMap.get("隐私协议地址"));
        appAboutUsVo.setUserAgreementUrl(configMap.get("用户协议地址"));
        appAboutUsVo.setWorkTime(configMap.get("工作时间"));
        return success(appAboutUsVo);
    }

    /**
     * POST /api/app/mini/check-version : app版本检查
     */
    @RequestMapping(value = "/check-version",
            method = RequestMethod.POST)
    @PermitAll
    public CommonResult checkVersion() {
        List<AppConfigDO> configList = appConfigService.findConfigByKey(Arrays.asList("app版本信息"));
        if (configList.isEmpty()) {
            return error(-1, "获取不到app信息，请联系系统管理员");
        }
        return jsonDataResult(configList.get(0).getValue());
    }

    /**
     * 获取客服信息
     * @return
     */
    @RequestMapping(value = "/customer-service-info",
            method = RequestMethod.POST)
    public CommonResult getWxChatData(){
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if(loginUserId == null){
            return error(HttpStatus.UNAUTHORIZED.value(), "账号未登录");
        }
        AppUserDO loginUser = getLoginUser(loginUserId);
        return success(appCustomerServiceComponent.getCustomerServiceInfo(loginUser));
    }

}
